//
//  ListiniArticoloVC.m
//  iTeVe
//
//  Created by Pedro Gonzalez on 12/04/14.
//  Copyright (c) 2014 SISI Software. All rights reserved.
//

#import "ListiniArticoloVC.h"

@interface ListiniArticoloVC ()

@end

@implementation ListiniArticoloVC

@synthesize modelArticolo, listini;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self populateListiniArticoli:modelArticolo.codart];
    
}


-(void) populateListiniArticoli:(NSString *)filtro
{
    listiniArticolo = [[NSMutableArray alloc] init];
//    listini = [[NSMutableArray alloc] init];
    NSMutableArray *tmpListiniArticolo = [[NSMutableArray alloc] init];
    
    FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
    listini = [db getListini];
    for (int i=0; i<listini.count; i++) {
        ListinoArticolo *tmpListinoArticolo = [[ListinoArticolo alloc] init];
        Listino *tmplis = listini[i];
        tmpListinoArticolo.codart = modelArticolo.codart;
        tmpListinoArticolo.codlis = tmplis.codlis;
        ListinoArticolo *lisart = [db getListinoArticoloxCodice:tmplis.codlis codiceArticolo:modelArticolo.codart];
        lisart.codart = modelArticolo.codart;
        lisart.codlis = tmplis.codlis;
        
        if (lisart.id==0){
            tmpListinoArticolo.prezzo = 0;
        } else {
            tmpListinoArticolo.prezzo = lisart.prezzo;
        }
        [listiniArticolo addObject:lisart];
    }
    
    tmpListiniArticolo = [db getListiniVenditaArticolo:modelArticolo];
    
    
//    articoli = [db getArticoli:filtro];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListinoArticoloCell";
    
    ListinoArticoloCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ListinoArticolo *listinoArticolo = [listiniArticolo objectAtIndex:[indexPath row]];
    
    Listino *listino = [[Listino alloc] init];
    listino = [listini objectAtIndex:[indexPath row]];
    cell.codice.text = listinoArticolo.codlis;
    cell.descrizione.text = listino.descrizione;
   
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setPositiveFormat:@"0.00"];
    
    cell.prezzo.text = [numberFormatter stringFromNumber:listinoArticolo.prezzo];
    return cell;
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [listiniArticolo count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListinoArticolo *listinoArticolo = [listiniArticolo objectAtIndex:[indexPath row]];
    [self.delegate selectItemViewController2:self didFinishEnteringItem:listinoArticolo];
    
}

@end
